<?php

namespace App\Admin\Controllers;

use App\Admin\Repositories\UserCar;
use Dcat\Admin\Form;
use Dcat\Admin\Grid;
use Dcat\Admin\Show;
use Dcat\Admin\Http\Controllers\AdminController;

class UserCarController extends AdminController
{
    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        return Grid::make(UserCar::with(['car', 'user', 'brand']), function (Grid $grid) {
            $grid->column('id')->sortable();
            $grid->column('user.name', admin_trans('user-car.fields.user_id'));
            $grid->column('brand.title', admin_trans('user-car.fields.brand_id'));
            $grid->column('car.title', admin_trans('user-car.fields.car_id'));
            $grid->column('car_number');
            $grid->column('car_engine');
            $grid->column('car_frame');
            $grid->column('updated_at')->sortable();
            $grid->model()->orderBy('id', 'desc');
        
            $grid->filter(function (Grid\Filter $filter) {
                $filter->equal('id');
                $filter->like('user.name', admin_trans('user-car.fields.user_id'));
                $filter->like('car.title', admin_trans('user-car.fields.car_id'));
            });

            $grid->disableEditButton();
            $grid->disableDeleteButton();
            $grid->disableCreateButton();
        });
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     *
     * @return Show
     */
    protected function detail($id)
    {
        return Show::make($id, UserCar::with(['car', 'user', 'brand']), function (Show $show) {
            $show->field('id');
            $show->field('user.name', admin_trans('user-car.fields.user_id'));
            $show->field('brand.title', admin_trans('user-car.fields.brand_id'));
            $show->field('car.title', admin_trans('user-car.fields.car_id'));
            $show->field('car_number');
            $show->field('car_engine');
            $show->field('car_frame');
            $show->field('car_license')->unescape()->as(function ($car_license) {
                if($car_license != "[]") {
                    $json_arr = json_decode($car_license, true);
                    return "<img width=200 src='".$json_arr[0]['url']."' />";
                }
            });
            $show->field('created_at');
            $show->field('updated_at');

            $show->panel()->tools(function ($tools) {
                $tools->disableDelete();
                $tools->disableEdit();
            });
        });
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        return Form::make(new UserCar(), function (Form $form) {
            $form->display('id');
            $form->text('user_id');
            $form->text('car_id');
            $form->text('car_number');
            $form->text('car_engine');
            $form->text('car_frame');
            $form->text('car_license');
        
            $form->display('created_at');
            $form->display('updated_at');
        });
    }
}
